﻿using SharpSvn;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SvnManager.SvnUpdate
{
	public partial class Form1 : Form
	{
		string svnPath = @"D:\yzzProject\scxuebaHtml5_Up";
		string fileName = "_serverUpdateFullPaths.json";
		string localPath = @"D:\yzzProject\scxuebaHtml5_Up\Project\SC.Weixin.Web\www";

		bool isLocal = false;

		string serverPath = "";
		string backPath = "";

		public Form1()
		{
			InitializeComponent();
			if (isLocal)
			{
				serverPath = @"D:\yzzProject\test\www";
				backPath = @"D:\yzzProject\test\back";
			}
			else
			{
				serverPath = @"D:\WeixinSite\merge";
				backPath = @"D:\WeixinSite\merge_back";
			}
		}

		//1、本地统一拷贝操作
		private void button1_Click(object sender, EventArgs e)
		{
			SvnClient client = new SvnClient();
			SvnStatusArgs sa = new SvnStatusArgs();
			client.GetStatus(svnPath, sa, out Collection<SvnStatusEventArgs> status);
			//删除的先不用管
			var localUpdateFullPaths = status.Where(m => new List<SvnStatus>() { SvnStatus.Modified, SvnStatus.Added }.Any(k => k == m.LocalNodeStatus)).Select(m => m.FullPath);

			if (localUpdateFullPaths.Any())
			{
				var debugLocalPath = Path.Combine(Environment.CurrentDirectory, "_local");
				if (Directory.Exists(debugLocalPath))
				{
					Directory.Delete(debugLocalPath, true);
				}

				//本地统一拷贝
				localUpdateFullPaths.ToList().ForEach(item =>
				{
					var path = item.Replace(localPath + "\\", "");
					//var copyPath = Path.Combine(backPath, path); ;

					var copyPath = Path.Combine(debugLocalPath, path);
					var parent = Directory.GetParent(copyPath);
					if (!parent.Exists)
					{
						parent.Create();
					}
					File.Copy(item, copyPath, true);
				});
				//将本地路径转化为服务器路径
				var serverUpdateFullPaths = localUpdateFullPaths.Select(m => m.Replace(localPath, serverPath));
				//将服务器文件路径集合以json文件的形式保存到文件中
				var path2 = Path.Combine(Environment.CurrentDirectory, fileName);
				//path2 = System.Environment.CurrentDirectory   "serverUpdateFullPaths.json";
				//Path.Combine(path2, "serverUpdateFullPaths.json")
				FileHelper.Write(path2, serverUpdateFullPaths.JsonSerialize(), Encoding.UTF8);
				MessageBox.Show("本地文件copy成功！");
			}
			else
			{
				MessageBox.Show("本地么有要copy的东西");
			}



			#region 、
			//foreach (var item in status)
			//{
			//	string fPath = item.FullPath;
			//	if (item.LocalContentStatus != item.RemoteContentStatus)
			//	{
			//		//被修改了的文件
			//	}
			//	//if (!item.Versioned)
			//	//{
			//	//	//新增文件
			//	//	client.Add(fPath);
			//	//}
			//	//else if(item.Conflicted)
			//	//{
			//	//	//将冲突的文件用工作文件解决冲突
			//	//	client.Resolve(fPath, SvnAccept.Working);
			//	//}
			//	//else if(item.IsRemoteUpdated)

			//	//{
			//	//	//更新来自远程的新文件
			//	//	client.Update(fPath);
			//	//}
			//	//else if(item.LocalContentStatus == SvnStatus.Missing)
			//	//{
			//	//	//删除丢失的文件
			//	//	client.Delete(fPath);
			//	//}
			//} 
			#endregion
		}

		//2、服务器备和更新操作
		private void button2_Click(object sender, EventArgs e)
		{
			var path2 = Path.Combine(System.Environment.CurrentDirectory, fileName);
			var serverUpdateFullPaths = File.ReadAllText(path2).JsonDeserialize<List<string>>();
			var fileInfoPathLi = serverUpdateFullPaths.Select(fullPath => new FileInfo(fullPath))
				.Where(m => m.Exists && m.LastWriteTime < DateTime.Now.Date).Select(m => m.FullName);
			if (fileInfoPathLi.Any())
			{
				fileInfoPathLi.ToList().ForEach(item =>
				{
					var path = item.Replace(serverPath + "\\", "");
					var copyPath = Path.Combine(backPath, DateTime.Now.ToString("yyyy-MM-dd") + " " + "yuanzhuangzhuang", path);
					var parent = Directory.GetParent(copyPath);
					if (!parent.Exists)
					{
						parent.Create();
					}
					File.Copy(item, copyPath, true);
				});
				MessageBox.Show("服务器备份成功！");
			}
			else
			{
				MessageBox.Show("服务器没有要备份的文件！");
			}

			//更新
			var debugLocalPath = Path.Combine(Environment.CurrentDirectory, "_local");
			var localFilenames = Directory.GetFiles(debugLocalPath, "*", SearchOption.AllDirectories);
			if (localFilenames.Any())
			{
				localFilenames.ToList().ForEach(item =>
				{
					var path = item.Replace(debugLocalPath + "\\", "");
					var serverRealPath = Path.Combine(serverPath, path);
					File.Copy(item, serverRealPath, true);
				});
				MessageBox.Show("服务器更新成功！");
			}
			else
			{
				MessageBox.Show("本地没有要更新的文件！");
			}
		}

		//获取日志信息
		private void button3_Click(object sender, EventArgs e)
		{
			SvnClient client = new SvnClient();
			SvnLogArgs logArgs = new SvnLogArgs
			{
				RetrieveAllProperties = false,//不检索所有属性
				Limit = 10,
				

			};
			client.GetLog(svnPath, logArgs, out Collection<SvnLogEventArgs> status);
		}

	}
}
